﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;
using System.Data.OracleClient;
using Model;

namespace BLL.ResourceManage
{
    public class ResourceBase
    {
        string strViewName = "";              //资源视图名称
        string strTableName = "";             //资源表名称
        string strReSelect;
        string strCTableName = "";          //资源中文名称
        string strHidAdd = "";

        string autoCodeColumns = "";          //存储自动编码英文字段
        string autoCodeColNames = "";         //存储自动编码中文字段名
        string autoCodeColType = "";          //存储自动编码字段的valuetype字段
      


        string sqlSelectComm = "";         //通用调用
        string gridShowColumnsComm = "";   //
        string gridShowFiledComm = "";      //
        string sqlFromComm = "";
        string sqlWhereComm = "";

        string temptablename = "";              //区别表和试图

        string unAutoCodeColumns = "";      //存储非自动编码英文字段
        string unAutoCodeColNames = "";     //存储非自动编码中文字段名
        string unAutoCodeColType = "";      //存储非自动编码字段的valuetype字段
        string unAutoCodeColRemark = "";    //存储非自动编码字段的valuetype字段
        string unAutoCodeColIsNull = "";    //存储非自动编码字段是否可空
        string unAutoCodeDataType = "";
       // string unAutoPathFile = "";             //存储非自动编码上传文件路径
      

        string RagatoryEFiled = "";              //可查询列的英文字段组合
        string RagatoryCFiled = "";              //可查询列的中文字段组合

        string columnsCNName = "";       //所有列以“,”分割
        string columnsENName = "";        //所有列以“,”分割

        string columns;                  // 所有以表名.列以“,”分割

        string strKeyName;             // 存储联合主键的名字，取最后一个主键列的值
        string strKey;                  // 存储联合主键，各主键名值对以“$”为分隔符，名值以“=”为分隔符
        string strUnionKeys = "";       //存储联合主键
        string keyValue;                //存储联合主键的名字，取最后一个主键列的值
        string keyName;

        string strIsAdditive = "";     //是否添加
        string strIsEditable = "";      //是否修改
        string strIsDelete = "";        //是否删除

        string gridShowColumns = "";    //grid 显示列
        string gridShowFiled = "";  //grid id





        string isOrderBy = "";              //是否排序
        string reSelect = "";



        private readonly OracleDAL.ResourceManage.ResourceBase dal = new OracleDAL.ResourceManage.ResourceBase();
        private readonly OracleDAL.ResourceManage.ResAdd ra = new OracleDAL.ResourceManage.ResAdd();
        #region  成员方法
        #endregion
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.ResourceManage.ResourceBase GetModel(string RESID)
        {

            return dal.GetModel(RESID);
        }

        ///************************************************
        /// 过程名称: getResQTName
        /// 功能描述: 根据资源id（resid）返回查询的视图名,资源表
        /// 参数列表
        ///  resid 资源id
        /// 作者    : 
        /// 创建日期: 2009/02/25
        /// ************************************************
        /// 
        #region ' 根据资源id（resid）返回查询的视图名,资源表'


        public void getResQTName(string resid)
        {
            // Model.ResourceManage.ResourceBase M = new OracleDAL.ResourceManage.ResourceBase().GetModel(resid);
            Model.ResourceManage.ResourceBase M = dal.GetModel(resid);
            strViewName = M.QUERYFROMVIEW;
            strTableName = M.MODIFYINFOTABLE;
            strCTableName = M.VIEWINFO;
            strHidAdd = M.ISADDITIVE;


            isOrderBy = M.ISORDER;


            //if (isTable())
            //{
            //返回自动编码的资源显示列，以逗号分割
            DataSet ds = new DataSet();
            ds = new OracleDAL.ResourceManage.ResAdd().GetList("isadditive='Y' and isautocode='Y' and tablename='" + strTableName + "' order by serial");
            for (int autoItem = 0; autoItem <= ds.Tables[0].Rows.Count - 1; autoItem++)
            {
                autoCodeColumns = autoCodeColumns + ds.Tables[0].Rows[autoItem]["enfield"].ToString().Trim() + ";";
                autoCodeColNames = autoCodeColNames + ds.Tables[0].Rows[autoItem]["chfield"].ToString().Trim() + ";";
                autoCodeColType = autoCodeColType + ds.Tables[0].Rows[autoItem]["valuetype"].ToString().Trim() + ";";
            }
            if (autoCodeColumns != "")
            {
                autoCodeColumns = autoCodeColumns.Substring(0, autoCodeColumns.Length - 1);
                autoCodeColNames = autoCodeColNames.Substring(0, autoCodeColNames.Length - 1);
                autoCodeColType = autoCodeColType.Substring(0, autoCodeColType.Length - 1);
            }
            //返回非自动编码的资源显示列，以逗号分割
            ds = new OracleDAL.ResourceManage.ResAdd().GetList("isadditive='Y' and isautocode='N' and tablename='" + strTableName + "' order by serial");
            for (int unAutoItem = 0; unAutoItem <= ds.Tables[0].Rows.Count - 1; unAutoItem++)
            {
                unAutoCodeColumns = unAutoCodeColumns + ds.Tables[0].Rows[unAutoItem]["enfield"].ToString().Trim() + ";";
                unAutoCodeColNames = unAutoCodeColNames + ds.Tables[0].Rows[unAutoItem]["chfield"].ToString().Trim() + ";";
                unAutoCodeColType = unAutoCodeColType + ds.Tables[0].Rows[unAutoItem]["valuetype"].ToString().Trim() + ";";
                unAutoCodeColIsNull = unAutoCodeColIsNull + ds.Tables[0].Rows[unAutoItem]["isnull"].ToString().Trim() + ";";
                unAutoCodeColRemark = unAutoCodeColRemark + ds.Tables[0].Rows[unAutoItem]["remark"].ToString().Trim() + ",";
                unAutoCodeDataType = unAutoCodeDataType + ds.Tables[0].Rows[unAutoItem]["datatype"].ToString().Trim() + ",";
               // unAutoPathFile = unAutoPathFile + ds.Tables[0].Rows[unAutoItem]["ispathfile"].ToString().Trim() + ",";

            }
            if (unAutoCodeColumns != "")
            {
                unAutoCodeColumns = unAutoCodeColumns.Substring(0, unAutoCodeColumns.Length - 1);
                unAutoCodeColNames = unAutoCodeColNames.Substring(0, unAutoCodeColNames.Length - 1);
                unAutoCodeColType = unAutoCodeColType.Substring(0, unAutoCodeColType.Length - 1);
                unAutoCodeColIsNull = unAutoCodeColIsNull.Substring(0, unAutoCodeColIsNull.Length - 1);
                unAutoCodeColRemark = unAutoCodeColRemark.Substring(0, unAutoCodeColRemark.Length - 1);
                unAutoCodeDataType = unAutoCodeDataType.Substring(0, unAutoCodeDataType.Length - 1);
               // unAutoPathFile = unAutoPathFile.Substring(0, unAutoPathFile.Length - 1);
            }
            ds.Clear();

            //  }
        }
        #endregion

        ///****************************************************
        ///*函数名称: getNetWorkRes
        ///*功能描述: 得到网系的资源列表
        ///*参数列表: 1.网系ID
        ///*作者    : 
        /// *创建日期: 2009/02/25
        ///  ************************************************   
        public ArrayList getNetWorkRes(string netid)
        {
            DataSet ds = new DataSet();
            string sqlNetWhere;
            ArrayList arrNet = new ArrayList();
            sqlNetWhere = "networkid='" + netid + "'";
            ds = dal.GetList(sqlNetWhere);
            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
                {
                    arrNet.Add(ds.Tables[0].Rows[i]["resid"].ToString().Trim() + "," + ds.Tables[0].Rows[i]["viewinfo"].ToString().Trim());
                }
            }

            return arrNet;
        }


        ///****************************************************
        ///*函数名称: getNetWorkRes
        ///*功能描述: 得到网系的资源列表
        ///*参数列表: 1.网系ID
        ///*作者    : 
        /// *创建日期: 2009/02/25
        ///  ************************************************   
        public ArrayList getNetWorkResCom(string netid,string tname)
        {
            DataSet ds = new DataSet();
            string sqlNetWhere;
            ArrayList arrNet = new ArrayList();
            if (tname == "")
            {
                sqlNetWhere = "networkid='" + netid + "' order by to_number(hotkey)";
            }
            else
            {
                sqlNetWhere = "networkid='" + netid + "' and resid='" + tname + "' order by to_number(hotkey)";
            }
            ds = dal.GetList(sqlNetWhere);
            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
                {
                    arrNet.Add(ds.Tables[0].Rows[i]["resid"].ToString().Trim() + "," + ds.Tables[0].Rows[i]["viewinfo"].ToString().Trim());
                }
            }

            return arrNet;
        }



        //获得翻译字段翻译表
        public DataTable getTran(string tablename, string filed, string tran)
        {
            DataTable dt = new DataTable();
            string strSql = "select transtable ,transcol,transedcol from  cfg_resource_fields";
            strSql += " where enfield='" + filed + "' and tablename='" + tablename + "' and valuetype='" + tran + "' ";
            dt = dal.GetSelectControlValue(strSql).Tables[0];
            return dt;
        }
        ///****************************************************
        ///*函数名称: getVisibleFiled
        ///*功能描述: 获得可查询资源的中英文字段
        ///*参数列表: 1.资源表名
        ///*作者    : 
        /// *创建日期: 2009/03/12
        ///  ************************************************   
        public void getVisibleFiled(string tablename)
        {
            DataSet ds = new DataSet();
            string sqlWhere = "tablename='" + tablename + "' and  isragatory='Y' order by to_number(serial)";
            ds = ra.GetList(sqlWhere);
            for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
            {
                RagatoryEFiled += ds.Tables[0].Rows[i]["enfield"].ToString().Trim() + ",";
                RagatoryCFiled += ds.Tables[0].Rows[i]["chfield"].ToString().Trim() + ",";
            }
            if (RagatoryEFiled != "")
            {
                RagatoryEFiled = RagatoryEFiled.Substring(0, RagatoryEFiled.Length - 1);
                RagatoryCFiled = RagatoryCFiled.Substring(0, RagatoryCFiled.Length - 1);

            }
            ds.Clear();
        }
        public DataSet getTranField(string resid,string fieldname)
        {
            DataSet ds = new DataSet();
            string strSql = "select transtable,transcol,transedcol,remark,valuetype,enfield from cfg_resource_fields where chfield='" + fieldname + "' and tablename='" + resid + "'";
            return dal.GetSelectControlValue(strSql);
        }
        public void delResource(string tname, string keyid)
        {
            string[] tempArr;
            string keyName, keyValue, strSql;
            string sqlCondition = "";

            tempArr = keyid.Split('$');
            for (int i = 0; i < tempArr.Length; i++)
            {
                keyName = tempArr[i].Substring(0, tempArr[i].IndexOf("="));
                keyValue = tempArr[i].Substring(tempArr[i].IndexOf("=") + 1);

                sqlCondition = sqlCondition + keyName + "='" + keyValue + "' and ";
            }
            sqlCondition = sqlCondition.Substring(0, sqlCondition.Length - 5);

            if (tname == "TOPO_TESTLINE")//级联删除
            {
                strSql = "declare begin delete TOPO_TESTPOINT where " + sqlCondition + ";delete circle_geo where " + sqlCondition + "; end;";
                dal.GetExcutState(strSql);
            }
            else
            {
                ;
            }
            strSql = "delete " + tname + " where " + sqlCondition;
            //GetExcutState
            dal.GetExcutState(strSql);


        }
        ///****************************************************
        ///*函数名称: convertColShow
        ///*功能描述: 将resDetail_colShow.Value转换为resDataBindShow可理解的colshow
        ///*参数列表: 1.字段链接字符串
        ///*作者    : 
        /// *创建日期: 2009/03/12
        ///  ************************************************   
        public string convertColShow(string connetEfiled)
        {
            string colShow = "";            //showcolumn对应字符串
            string[] tmpArr;
            tmpArr = connetEfiled.Split(',');
            for (int i = 0; i < tmpArr.Length; i++)
            {
                if (colShow == "")
                {
                    colShow = tmpArr[i].Substring(tmpArr[i].IndexOf("|") + 1);
                }
                else
                {
                    colShow = colShow + "," + tmpArr[i].Substring(tmpArr[i].IndexOf("|") + 1);
                }
            }
            return colShow;
        }
        //获得权限字符串逗号相连
        public string operateStr(string strOperateID)
        {
            string strSql = "";
            string strRes = "";
            return strRes;
        }
        /// <summary>
        /// 是否可添加
        /// </summary>
        /// <param name="net">网系</param>
        /// <param name="name">资源名称</param>
        /// <returns></returns>
        public string getstrHidAdd(string net, string name)
        {
            // return strHidAdd;
            string hidadd = "";
            DataSet ds = new DataSet();
            string sql = "select ISADDITIVE from cfg_resource_info where networkid='" + net + "' and resid='" + name + "' ";
            ds = dal.GetSelectControlValue(sql);
            hidadd = ds.Tables[0].Rows[0]["ISADDITIVE"].ToString();
            return hidadd;

        }
        //************************************************
        //函数名称: convertCondition
        //功能描述: 将resDetail_allConditionID.Value转换为resDataBindShow可理解的Condition
        //参数列表:      
        //作者    : 
        //创建日期: 2009/03/12
        //************************************************
        public string convertCondition(string tablename, string ConditionID, string Condition2)
        {
            string strCondition = "";
            string[] tmpArr;
            string[] tmpArr2;
            string strViewName;

            if (ConditionID == "")
            { }
            else
            {
                tmpArr = ConditionID.Split(',');
                tmpArr2 = Condition2.Split(',');
                for (int i = 0; i <tmpArr.Length - 1; i++)
                {
                    if (tmpArr[i].Substring(tmpArr[i].IndexOf(".") + 1, 5).ToLower() == "date|")
                    {
                        //if(tmpArr[i].Substring(0,7)=="is null")
                        //{
                        //    //处理查询条件为空的情况
                        //    tmpArr2[i]=tmpArr2[i].Substring(0,2);
                        //}
                        //else
                        //{
                        //    if(tmpArr[i].Length>12)
                        //    {
                        //        if(tmpArr[i].Substring(0,11)=="is not null")
                        //        {
                        //            tmpArr2[i]=tmpArr2[i].Substring(0,2);
                        //        }
                        //        else
                        //        {
                        //if (tmpArr2[i].Substring(0, 2) == ">=" || tmpArr2[i].Substring(0, 2) == "<=")
                        //{
                        //   // string codstr = tmpArr2[i].Substring(0, 2).ToString();
                        //    tmpArr2[i] = tmpArr2[i].Substring(0, 2) + "to_date('" + tmpArr2[i].Substring(2) + "','yyyy-mm-dd hh24:mi:ss')";
                        //}
                        //else
                        //{
                        tmpArr2[i] = tmpArr2[i].Substring(0, 2) + "to_date('" + tmpArr2[i].Substring(2) + "','yyyy-mm-dd hh24:mi:ss')";
                        // }

                        //         tmpArr2[i]=tmpArr2[i].Substring(0,2) +"to_date('" + tmpArr2[i].Substring(2) + "','yyyy-mm-dd hh24:mi:ss')";
                        //        }
                        //    }
                        //}
                    }
                    else if (tmpArr[i].Substring(tmpArr[i].IndexOf(".") + 1, 7).ToLower() == "number|") //处理数字类型
                    {
                        if (tmpArr[i].Length > 8)
                        {
                            if (tmpArr[i].Substring(0, 7) == "is null")
                            {
                                //处理查询条件为空的情况
                                tmpArr2[i] = tmpArr2[i].Substring(0, 2);
                            }
                            else
                            {
                                if (tmpArr[i].Length > 12)
                                {
                                    if (tmpArr[i].Substring(0, 11) == "is not null")
                                    {
                                        //处理查询条件为空的情况
                                        tmpArr2[i] = tmpArr2[i].Substring(0, 2);
                                    }
                                    else
                                    {
                                        tmpArr2[i] = tmpArr2[i].Substring(0, 2) + tmpArr2[i].Substring(2);
                                    }
                                }

                            }
                        }
                    }
                    else
                    {
                        if (tmpArr[i].Length > 8)
                        {
                            if (tmpArr[i].Substring(0, 7) == "is null")
                            {
                                tmpArr2[i] = tmpArr2[i].Substring(0, 2);
                            }
                            else
                            {
                                if (tmpArr[i].Length > 12)
                                {
                                    if (tmpArr[i].Substring(0, 11) == "is not null")
                                    {
                                        //处理查询条件为空的情况
                                        tmpArr2[i] = tmpArr2[i].Substring(0, 2);
                                    }
                                    else
                                    {
                                        tmpArr2[i] = tmpArr2[i].Substring(0, 2) + "'" + tmpArr2[i].Substring(2) + "'";
                                    }
                                }

                            }
                        }

                    }
                    if (strCondition == "")
                    {
                        if (tmpArr[i].Substring(tmpArr[i].IndexOf(".") + 1, 5).ToLower() == "date|")
                        {
                            // strCondition = tablename + "." + tmpArr[i].Substring(tmpArr[i].IndexOf("|") + 1) + " " + tmpArr[i].Substring(0, tmpArr[i].IndexOf(".")) + tmpArr2[i].Substring(2);
                            strCondition = tmpArr[i].Substring(tmpArr[i].IndexOf("|") + 1) + " " + tmpArr[i].Substring(0, tmpArr[i].IndexOf(".")) + tmpArr2[i].Substring(1);
                        }
                        else
                        {
                            //strCondition = tablename + "." + tmpArr[i].Substring(tmpArr[i].IndexOf("|") + 1) + " " + tmpArr[i].Substring(0, tmpArr[i].IndexOf(".")) + "" + tmpArr2[i].Substring(2) + "";
                            strCondition = tmpArr[i].Substring(tmpArr[i].IndexOf("|") + 1) + " " + tmpArr[i].Substring(0, tmpArr[i].IndexOf(".")) + "" + tmpArr2[i].Substring(2) + "";
                        }
                    }
                    else
                    {
                        if (tmpArr[i].Substring(tmpArr[i].IndexOf(".") + 1, 5).ToLower() == "date|")
                        {
                            // strCondition = strCondition + " and " + tablename + "." + tmpArr[i].Substring(tmpArr[i].IndexOf("|") + 1) + " " + tmpArr[i].Substring(0, tmpArr[i].IndexOf(".")) + tmpArr2[i].Substring(2);
                            strCondition = strCondition + " and " + tmpArr[i].Substring(tmpArr[i].IndexOf("|") + 1) + " " + tmpArr[i].Substring(0, tmpArr[i].IndexOf(".")) + tmpArr2[i].Substring(2);
                        }
                        else
                        {
                            // strCondition = strCondition + " and " + tablename + "." + tmpArr[i].Substring(tmpArr[i].IndexOf("|") + 1) + " " + tmpArr[i].Substring(0, tmpArr[i].IndexOf(".")) + "" + tmpArr2[i].Substring(2) + "";
                            strCondition = strCondition + " and " + tmpArr[i].Substring(tmpArr[i].IndexOf("|") + 1) + " " + tmpArr[i].Substring(0, tmpArr[i].IndexOf(".")) + "" + tmpArr2[i].Substring(2) + "";
                        }
                    }
                }
            }
            return strCondition;
        }
        //*************************************************************************
        //	函数名称:	isColInTable
        //	功能描述:	table中是否有column字段
        //	参数列表:	
        //	作    者:	
        //	编写日期:	2009/03/12   17:34:53
        //*************************************************************************
        public bool isColInTable(string column, string table)
        {
            DataSet ds = new DataSet();
            string strSql = "select column_name from user_tab_Columns where table_name=upper('" + table + "') and column_name=upper('" + column + "')";
            ds = dal.GetSelectControlValue(strSql);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return true;
            }
            else { return false; }
        }
        public DataTable getDataTable(string sql)
        {
            DataTable dt = new DataTable();
            dt = dal.GetSelectControlValue(sql).Tables[0];
            return dt;
        }
        public DataSet getColCnname(string resid)
        {
            string strSql = "select CHFIELD,ENFIELD,VALUETYPE from cfg_resource_fields where isvisible ='Y' and ISRAGATORY='Y' and tablename='" + resid + "'";
            return dal.GetSelectControlValue(strSql);
        }
        //************************************************
        //*函数名称: getColCNName
        //*功能描述: 获取中文显示字段名
        //*参数列表:
        //*作者    : 
        //*创建日期: 
        //************************************************
        public string getColCNName()
        {
            DataSet ds = new DataSet();
            string[] colsArr;
            colsArr = columnsENName.Split(',');
            for (int i = 0; i < colsArr.Length; i++)
            {
                string strSql = "select CHFIELD from cfg_resource_fields where upper(enfield) =upper('" + colsArr[i].ToString().Trim() + "') and tablename='" + strTableName + "'";
                ds = dal.GetSelectControlValue(strSql);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    if (columnsCNName == "")
                    {
                        columnsCNName = ds.Tables[0].Rows[0]["CHFIELD"].ToString().Trim();
                    }
                    else
                    {
                        columnsCNName = columnsCNName + "," + ds.Tables[0].Rows[0]["CHFIELD"].ToString().Trim();
                    }
                }
            }
            ds.Clear();
            return columnsCNName;
        }
        //public string getColCNName()
        //{
        //    DataSet ds = new DataSet();
        //    string[] colsArr;
        //    colsArr=columnsENName.Split(',');
        //    for (int i = 0; i < colsArr.Length; i++)
        //    {
        //        string strSql = "select CHFIELD from cfg_resource_fields where upper(enfield) =upper('" + colsArr[i].ToString().Trim() + "') and tablename='" + strTableName + "'";
        //        ds = dal.GetSelectControlValue(strSql);
        //        if (ds.Tables[0].Rows.Count > 0)
        //        {
        //            if (columnsCNName == "")
        //            {
        //                columnsCNName = ds.Tables[0].Rows[0]["CHFIELD"].ToString().Trim();
        //            }
        //            else
        //            {
        //                columnsCNName = columnsCNName + "," + ds.Tables[0].Rows[0]["CHFIELD"].ToString().Trim();
        //            }
        //        }
        //    }
        //    ds.Clear();
        //    return columnsCNName;
        //}
        public string getColENName()
        {
            return columns.Substring(0, columns.Length - 1);
        }

        ///****************************************************
        ///*函数名称: buildWhereContor
        ///*功能描述: 得到网系的资源列表
        ///*参数列表: 1.网系ID
        ///*作者    : 
        /// *创建日期: 2009/02/25
        ///  ************************************************   
        public DataSet buildWhereContor(string resname, string resid)
        {

            DataSet ds = new DataSet();
            string sqlWhere = "tablename='" + resid + "' and isaotoselect is not null order by isaotoselect";
            ds = ra.GetList(sqlWhere);
            return ds;
        }
        ///****************************************************
        ///*函数名称: buildQuerySqlString
        ///*功能描述: 修改的查询语句组合
        ///*参数列表: 
        ///*作者    : 
        /// *创建日期: 2009/03/12
        ///  ************************************************   
        public string buildQuerySqlString(string resid, string showcolumn, string sqlCondition, string strSort, string strSortDirection,string nodeWhere)
        {
            DataSet ds = new DataSet();
            string sqlSelect = "";
            string sqlFrom = "";
            string sqlWhere = "";
            string strSql = "";
            string[] strCols;

            getResQVTName(resid);

            int num1 = sqlCondition.IndexOf(",");
            //处理资源基本表中是否有nodeid字段
            if (num1 == -1 && sqlCondition != "")
            {
                string tabName, colName;
                num1 = sqlCondition.IndexOf(".");
                //获取资源基本表名
                // tabName = sqlCondition.Substring(0,num1);
                //获取资源节点列
                int num2 = sqlCondition.IndexOf(" ", num1 + 1);
                colName = sqlCondition.Substring(num1 + 1, num2 - num1 - 1);
                if (colName.ToUpper() == "nodeid".ToUpper())
                {
                    strSql = "select column_name from user_tab_Columns where table_name=upper('" + strTableName + "') and column_name=upper('" + colName + "')";
                    ds = dal.GetSelectControlValue(strSql);
                    if (ds.Tables[0].Rows.Count == 0)
                    {
                        sqlCondition = "";
                    }
                    ds.Clear();

                }
            }
            if (isTable())
            {
                temptablename = strTableName;
            }
            else
            {
                temptablename = strViewName;
            }


            //翻译某些字段
            //if (isTable())//从表中查的话需要转化一些字段（例如将内码转换为用户理解的汉字）
            //{

            //提取出主键，添加iskey作为查询字段
            strSql = "select enfield from cfg_resource_fields where iskey='Y' and tablename='" + strTableName + "' order by serial";
            ds = dal.GetSelectControlValue(strSql);
            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int m = 0; m < ds.Tables[0].Rows.Count; m++)
                {
                    strKey = strKey + "'" + ds.Tables[0].Rows[m]["enfield"].ToString().Trim() + "='||" + temptablename + "." + ds.Tables[0].Rows[m]["enfield"].ToString().Trim() + "||'$'||";
                    strKeyName = ds.Tables[0].Rows[m]["enfield"].ToString().Trim();

                }

            }
            ds.Clear();
            //组装联合主键
            if (strKey != "")
            {
                strKey = strKey.Substring(0, strKey.Length - 7);
                strUnionKeys = strKey;
                strKey = strKey + " as id";
            }
            //处理showcolumn为""的情况,显示所有"查询可见"的字段
            if (showcolumn == "")
            {
                strSql = "select enfield from cfg_resource_fields where isvisible='Y' and tablename='" + strTableName + "' order by serial";
                ds = dal.GetSelectControlValue(strSql);
                for (int n = 0; n < ds.Tables[0].Rows.Count; n++)
                {
                    if (showcolumn == "")
                    {
                        showcolumn = ds.Tables[0].Rows[n]["enfield"].ToString();
                    }
                    else
                    {
                        showcolumn = showcolumn + "," + ds.Tables[0].Rows[n]["enfield"].ToString();
                    }
                }
                ds.Clear();
            }
            //将联合主键名放到columnsENName中
            if (strKeyName != "")
            {
                columnsENName = strKeyName + "," + showcolumn;
            }
            else
            {
                columnsENName = showcolumn;
            }
            //  strCols = showcolumn.Split(',');
            sqlFrom = temptablename + ",";       //添加资源基本表名
            //for (int i = 0; i < strCols.Length; i++)
            //{
            strSql = "select chfield,enfield,transtable,transcol,transedcol,remark,valuetype from cfg_resource_fields where tablename='" + strTableName + "' and isvisible='Y' order by serial";
            ds = dal.GetSelectControlValue(strSql);
            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int index = 0; index < ds.Tables[0].Rows.Count; index++)
                {



                    if (ds.Tables[0].Rows[index]["valuetype"].ToString().Trim() != "string" && ds.Tables[0].Rows[index]["valuetype"].ToString().Trim() != "date" && ds.Tables[0].Rows[index]["valuetype"].ToString().Trim() != "int" && ds.Tables[0].Rows[index]["valuetype"].ToString().Trim() != "number" && ds.Tables[0].Rows[index]["valuetype"].ToString().Trim() != "filename")
                    {
                        string strtt = ds.Tables[0].Rows[index]["transtable"].ToString().Trim();//翻译表名
                        string strts = ds.Tables[0].Rows[index]["transcol"].ToString().Trim();//翻译id
                        string strtd = ds.Tables[0].Rows[index]["transedcol"].ToString().Trim();//翻译name

                        string[] transedArr = ds.Tables[0].Rows[index]["valuetype"].ToString().Trim().Split(',');
                        if (transedArr.Length > 1)
                        {
                            sqlSelect = sqlSelect + "(select " + strtt + "." + strtd + " from " + strtt;
                            sqlSelect += " where " + strtt + "." + strts + "=" + temptablename + "." + ds.Tables[0].Rows[index]["enfield"].ToString().Trim() + " ) as \"" + ds.Tables[0].Rows[index]["chfield"].ToString().Trim() + "\",";


                            sqlSelect += "(select " + strtt + "." + strts + " from " + strtt;
                            sqlSelect += " where " + strtt + "." + strts + "=" + temptablename + "." + ds.Tables[0].Rows[index]["enfield"].ToString().Trim() + " ) as \"" + ds.Tables[0].Rows[index]["chfield"].ToString().Trim() + "编号\",";

                            gridShowColumns = gridShowColumns + ds.Tables[0].Rows[index]["chfield"].ToString().Trim() + "," + ds.Tables[0].Rows[index]["chfield"].ToString().Trim() + "编号,";

                            gridShowFiled = gridShowFiled + ds.Tables[0].Rows[index]["enfield"].ToString().Trim() + "," + strtd + ",";
                        }
                        else
                        {

                            sqlSelect = sqlSelect + "(select " + strtt + "." + strtd + " from " + strtt;
                            sqlSelect += " where " + strtt + "." + strts + "=" + temptablename + "." + ds.Tables[0].Rows[index]["enfield"].ToString().Trim() + " ) as \"" + ds.Tables[0].Rows[index]["chfield"].ToString().Trim() + "\",";

                            gridShowColumns = gridShowColumns + ds.Tables[0].Rows[index]["chfield"].ToString().Trim() + ",";

                            gridShowFiled = gridShowFiled + ds.Tables[0].Rows[index]["enfield"].ToString().Trim() + ",";


                        }
                    }
                    else
                    {
                        sqlSelect = sqlSelect + ds.Tables[0].Rows[index]["enfield"].ToString().Trim() + " as \"" + ds.Tables[0].Rows[index]["chfield"].ToString().Trim() + "\",";

                        gridShowColumns = gridShowColumns + ds.Tables[0].Rows[index]["chfield"].ToString().Trim() + ",";

                        gridShowFiled = gridShowFiled + ds.Tables[0].Rows[index]["enfield"].ToString().Trim() + ",";

                    }
                    ////需要翻译的话，添加联查表名和联查条件
                    ////为了处理同一表中出现多于两个翻译字段相同的情况，这样能保证列名唯一
                }
            }

            ds.Clear();
            //处理sqlfrom中出复出现的表名
            string[] strArrFrom;
            //
            //xiugai
            sqlFrom = temptablename;
            strArrFrom = sqlFrom.Split(',');
            string[] tmpArr;
            //未加入去掉重复字段


            //'添加条件
            if (sqlCondition == "")
            {
                sqlWhere = sqlCondition;
            }
            else
            {
                sqlWhere = sqlCondition + " and ";
            }

            // '处理字符串最后的逗号
            if (sqlSelect != "")
            {
                sqlSelect = sqlSelect + reSelect;//reSelect 排序字段
                sqlSelect = sqlSelect.Substring(0, sqlSelect.Length - 1);
            }
            if (sqlFrom != "")
            {
                // sqlFrom = sqlFrom.Substring(0, sqlFrom.Length - 1);
            }
            //
            sqlWhere = sqlWhere + "1=1";
            if (strKey != "")
            {
                gridShowColumns = "id" + "," + gridShowColumns;
                gridShowFiled = "id" + "," + gridShowFiled;
            }
            else
            {
                gridShowColumns = gridShowColumns;
                gridShowFiled = gridShowFiled;
            }
            //}
            //else     //视图的话——视图暂时没有处理
            //{
            //        columnsENName = showcolumn;
            //        columns = showcolumn;
            //        sqlSelect = showcolumn;
            //        sqlFrom = strViewName;
            //        if( sqlCondition == "" )
            //        {
            //            sqlWhere = " 1=1";
            //        }
            //        else
            //        {
            //            sqlWhere = sqlCondition;
            //        }

            //}
            //返回的查询sql语句
            if (strKey != "" || strKey != null)
            {
                if (strSort != "" || strSortDirection != "")
                {
                    strSql = "select * from (select distinct " + strKey + "," + sqlSelect + " from " + sqlFrom + " where " + nodeWhere +  " ) where " + sqlWhere + strSort + " " + strSortDirection;
                }
                else
                {
                    strSql = "select * from (select distinct " + strKey + "," + sqlSelect + " from " + sqlFrom + (nodeWhere.Trim().Length == 0 ? "" : " where " + nodeWhere) + " ) where " + sqlWhere;
                }

            }
            else
            {
                if (strSort != "" || strSortDirection != "")
                {
                    strSql = "select * from (select distinct " + sqlSelect + " from " + sqlFrom + " where " + nodeWhere + ") where " + sqlWhere + strSort + " " + strSortDirection;
                }
                else
                {
                    strSql = "select * from (select distinct " + sqlSelect + "  from " + sqlFrom + " where " + nodeWhere +  ") where " + sqlWhere;

                }
            }


            return strSql;


        }
        public void getResQVTName(string resid)
        {
            DataSet ds = new DataSet();

            string strSql = "select * from cfg_resource_info where resid='" + resid + "'";
            ds = dal.GetSelectControlValue(strSql);
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                strTableName = ds.Tables[0].Rows[i]["MODIFYINFOTABLE"].ToString();
                strViewName = ds.Tables[0].Rows[i]["QUERYFROMVIEW"].ToString();
                strReSelect = ds.Tables[0].Rows[i]["ISORDER"].ToString();
                reSelect = ds.Tables[0].Rows[i]["ISORDER"].ToString();
                strIsAdditive = ds.Tables[0].Rows[i]["ISADDITIVE"].ToString();
                strIsEditable = ds.Tables[0].Rows[i]["ISEDITABLE"].ToString();
                strIsDelete = ds.Tables[0].Rows[i]["ISDELETE"].ToString();
            }
            if (strReSelect == "")
            {
                strReSelect = "order by COL2 DESC";
            }
            else
            {
                strReSelect = "order by " + strReSelect;
            }
            ds.Clear();
            ds.Dispose();

        }
        ///****************************************************
        ///*函数名称: getCommonResource
        ///*功能描述: 修改的查询语句组合
        ///*参数列表: resid 资源表名  showcolumn 显示列 可以为空  sqlCondition 条件 (表名.字段='') strSort  排序字段（order  by   表名.字段）
        ///*作者    : 
        /// *创建日期: 2009/03/12
        ///  ************************************************   
        public string  getCommonResource(string resid, string showcolumn, string sqlCondition, string strSort, string strSortDirection)
        {
            string strSql="";
            string strKeycomm="";
            
            string strKeyName="";

            DataSet ds = new DataSet();

            getResQVTName(resid);
             if (isTable())
            {
                temptablename = strTableName;
            }
            else
            {
                temptablename = strViewName;
            }



            strSql = "select enfield from cfg_resource_fields where iskey='Y' and tablename='" + temptablename + "' order by serial";
            ds = dal.GetSelectControlValue(strSql);
            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int m = 0; m < ds.Tables[0].Rows.Count; m++)
                {
                    strKeycomm = strKeycomm + "'" + ds.Tables[0].Rows[m]["enfield"].ToString().Trim() + "='||" + temptablename + "." + ds.Tables[0].Rows[m]["enfield"].ToString().Trim() + "||'$'||";
                    strKeyName = ds.Tables[0].Rows[m]["enfield"].ToString().Trim();

                }

            }
            ds.Clear();
                //组装联合主键
            if (strKey != "")
            {
                strKeycomm = strKeycomm.Substring(0, strKeycomm.Length - 7);
                strUnionKeys = strKeycomm;
                strKeycomm = strKeycomm + " as id,";
            }




         
            strSql = "select chfield,enfield,transtable,transcol,transedcol,remark,valuetype from cfg_resource_fields where tablename='" + temptablename + "' and isvisible='Y' order by serial";
            ds = dal.GetSelectControlValue(strSql);
            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int index = 0; index < ds.Tables[0].Rows.Count; index++)
                {


                    if (ds.Tables[0].Rows[index]["valuetype"].ToString().Trim() != "string" && ds.Tables[0].Rows[index]["valuetype"].ToString().Trim() != "date" && ds.Tables[0].Rows[index]["valuetype"].ToString().Trim() != "int" && ds.Tables[0].Rows[index]["valuetype"].ToString().Trim() != "number")
                    {
                        string strtt = ds.Tables[0].Rows[index]["transtable"].ToString().Trim();//翻译表名
                        string strts = ds.Tables[0].Rows[index]["transcol"].ToString().Trim();//翻译id
                        string strtd = ds.Tables[0].Rows[index]["transedcol"].ToString().Trim();//翻译name

                        sqlSelectComm = sqlSelectComm + strtt + index.ToString() + "." + strtd + " as \"" + ds.Tables[0].Rows[index]["chfield"].ToString().Trim() + "\" , ";
                        sqlFromComm = sqlFromComm + strtt + " " + strtt + index.ToString()+",";


                        sqlWhereComm = sqlWhereComm + resid + "." + ds.Tables[0].Rows[index]["enfield"].ToString().Trim() + "=" + strtt + index.ToString() + "." + strts + "(+) and ";
              
                    }


                    sqlSelectComm = sqlSelectComm + resid + "." + ds.Tables[0].Rows[index]["enfield"].ToString().Trim() + " as \"" + ds.Tables[0].Rows[index]["chfield"].ToString().Trim() + "\",";

                    sqlFromComm = sqlFromComm;
                    sqlWhereComm = sqlWhereComm;
                    gridShowColumnsComm = gridShowColumns + ds.Tables[0].Rows[index]["chfield"].ToString().Trim() + ",";

                    gridShowFiledComm = gridShowFiled + ds.Tables[0].Rows[index]["enfield"].ToString().Trim() + ",";
                }
                if (sqlSelectComm != "")
                {
                    sqlSelectComm = sqlSelectComm.Substring(0, sqlSelectComm.Length - 1);
                }
                if (sqlFromComm != "")
                {
                    sqlFromComm = temptablename + "," + sqlFromComm.Substring(0, sqlFromComm.Length - 1);
                }
                else {

                    sqlFromComm = resid;
                }
                if (sqlWhereComm != "")
                {
                    sqlWhereComm = sqlWhereComm.Substring(0, sqlWhereComm.Length - 4);
                }
                else
                {
                    sqlWhereComm = " 1=1 ";
                }
                if (strSort != "" || strSortDirection != "")
                {
                    strSql = "select  distinct " + strKeycomm + sqlSelectComm + " from " + sqlFromComm + "  where " + sqlWhereComm + sqlCondition + strSort + " " + strSortDirection;
                }
                else
                {
                    strSql = "select  distinct " + strKeycomm + sqlSelectComm + " from " + sqlFromComm + "  where " + sqlWhereComm + sqlCondition;
                }
                ds = dal.GetSelectControlValue(strSql);
            }
            return strSql;
 
        }
        public string getStrIsAdditive()
        {
            return strIsAdditive;
        }
        public string getStrIsEditable()
        {
            return strIsEditable;
        }
        public string getStrIsDelete()
        {
            return strIsDelete;
        }
        //排序
        public string getstrReSelect()
        {
            return strReSelect;
        }
        /// ************************************************
        /// *函数名称: isTable
        /// *功能描述: 判断表cfg_resource_info中查询用字段queryfromview值是表还是视图
        /// *参数列表:
        /// *作者    : 
        /// *创建日期: 2009/02/25
        ///  ************************************************   '
        public bool isTable()
        {
            if (strTableName == strViewName)
            {
                return true;
            }
            else
            {
                return false;
            }

        }
        public DataSet getSelectControlValue(string vid, string vname, string vtable)
        {
            string strSql = "select " + vid + "," + vname + " from " + vtable;
            return dal.GetSelectControlValue(strSql);

        }
        //构造显示列ID
        public string getGridShowFiled()
        {
            return gridShowFiled.Substring(0, gridShowFiled.Length - 1);
        }
        //构造显示列
        public string getgridShowColumns()
        {
            return gridShowColumns.Substring(0, gridShowColumns.Length - 1);
        }
        //返回可查询的列的英文字段链接字符串
        public string getRagatoryEFiled()
        {
            return RagatoryEFiled;
        }
        //返回可查询的列的中文字段链接字符串
        public string getRagatoryCFiled()
        {
            return RagatoryCFiled;
        }
        //返回资源表的中文名称
        public string getCTableName()
        {
            return strCTableName;
        }
        //返回所要操作的视图名称
        public string getViewName()
        {
            return strViewName;
        }
        //返回所要操作的表名称
        public string getTableName()
        {
            return strTableName;
        }
        //返回自动编码的资源显示列，以逗号分割
        public string getAutoCodeColumns()
        {
            return autoCodeColumns;
        }
        //
        public string getunAutoCodeDataType()
        {
            return unAutoCodeDataType;
        }
        //返回自动编码的资源显示列名
        public string getAutoCodeColNames()
        {
            return autoCodeColNames;
        }
        //返回自动编码的资源显示列名
        public string getUnPathFile()
        {
           // return unAutoPathFile;
            return "";
        }
        //返回自动编码的资源列的valuetype值，以逗号分割
        public string getAutoCodeColType()
        {
            return autoCodeColType;
        }
        //返回非自动编码的资源显示列，以逗号分割
        public string getUnAutoColumns()
        {
            return unAutoCodeColumns;
        }
        //返回非自动编码的资源中文显示列名，以逗号分割
        public string getUnAutoCodeColName()
        {
            return unAutoCodeColNames;
        }
        //返回非自动编码的资源列的valuetype值，以逗号分割
        public string getUnAutoCodeColType()
        {
            return unAutoCodeColType;
        }
        //返回非自动编码的资源列的Remark值，以逗号分割
        public string getUnAutoCodeColRemark()
        {
            return unAutoCodeColRemark;
        }
        //返回非自动编码的资源列isnull值，以逗号分割。即该字段是否可空
        public string getUnAutoCodeColIsNull()
        {
            return unAutoCodeColIsNull;
        }
        public string getIsOrderBy()
        {
            return isOrderBy;
        }
    }
}
